package com.btpn.custody.entity;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;

@Entity
@org.hibernate.annotations.Entity(dynamicInsert=true, dynamicUpdate=true)
@Table(name = "M_USER")
@NamedQueries({
	@NamedQuery(name  = "User.deleteByUsername",
				query = "DELETE FROM User u WHERE u.username = :username"),
				
	@NamedQuery(name  = "User.findByAreaId",
				query = "FROM User u WHERE u.area.id = :areaId"),	
				
	@NamedQuery(name  = "User.findByGroupRoleId",
				query = "FROM User u WHERE u.groupRole.id = :groupRoleId")
})
public class User implements Serializable {
	/*===================================== C O N S T A N T ===================================*/
	private static final long serialVersionUID = -9170723130152794209L;
	
	public static final String NQ__DELETE__BY__USERNAME = "User.deleteByUsername";
	
	public static final String NQ__FIND__BY__AREA_ID = "User.findByAreaId";
	
	public static final String NQ__FIND__BY__GROUPROLE_ID = "User.findByGroupRoleId";
	
	/*===================================== C O N S T R U C T O R =============================*/
	public User() {}
	
	/*===================================== M E M B E R =======================================*/
	@Id
	@Column(name = "USERNAME", unique=true)
	private String username;
	
	@Column(name = "PASSWORD")
	private String password;
	
	@Column(name = "ENABLED", columnDefinition="BIT DEFAULT 0")
	private boolean enabled = false;

	@Column(name = "IS_FIRST_LOGIN", columnDefinition="BIT DEFAULT 1")
	private boolean firstLogin = true;

	@Column(name = "EMAIL")
	private String email;
	
	@Column(name = "__CREATED_DATE")
	private Date createdDate = new Date();

	@Column(name = "__CREATED_BY")
	private String createdBy;

	@Column(name = "__UPDATED_DATE")
	private Date updatedDate = new Date();

	@Column(name = "__UPDATED_BY")
	private String updatedBy;
	
	@ManyToOne()
	@JoinColumn(name="AREA_ID")
	private Area area;
	
	@ManyToOne
	@JoinColumn(name="GROUP_ROLE_ID")
	private GroupRole groupRole;
	
	/*===================================== G E T T E R   &   S E T T E R =====================*/
	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public boolean isFirstLogin() {
		return firstLogin;
	}

	public void setFirstLogin(boolean firstLogin) {
		this.firstLogin = firstLogin;
	}

	public Area getArea() {
		return area;
	}

	public void setArea(Area area) {
		this.area = area;
	}

	public boolean isEnabled() {
		return enabled;
	}
	
	public void setEnabled(boolean enabled) {
		this.enabled = enabled;
	}
	
	public Date getCreatedDate() {
		return createdDate;
	}

	public void setCreatedDate(Date createdDate) {
		this.createdDate = createdDate;
	}

	public String getCreatedBy() {
		return createdBy;
	}

	public void setCreatedBy(String createdBy) {
		this.createdBy = createdBy;
	}

	public Date getUpdatedDate() {
		return updatedDate;
	}

	public void setUpdatedDate(Date updatedDate) {
		this.updatedDate = updatedDate;
	}

	public String getUpdatedBy() {
		return updatedBy;
	}

	public void setUpdatedBy(String updatedBy) {
		this.updatedBy = updatedBy;
	}

	public GroupRole getGroupRole() {
		return groupRole;
	}

	public void setGroupRole(GroupRole groupRole) {
		this.groupRole = groupRole;
	}

	/*===================================== O V E R R I D E    M E T H O D ====================*/	
	@Override
	public boolean equals(Object obj){ 

		if(obj == null) {
			return false;
		}
		if(obj.getClass() != getClass()){
			return false;
		}

		User emp = (User)obj;
		if(this.username == emp.username){
			return true;
		}
		return false;
	}

	@Override
	public int hashCode(){
		return username.hashCode();
	}
}
